package com.net.xpay.web.controller.common;

import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.model.PartnerUserFilingsSubmitModel;
import com.net.xpay.common.service.PartnerUserFilingsService;
import com.net.xpay.web.vo.UserVo;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.util.NetUtil;
import com.net.common.util.SerializationUtil;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.enums.config.CommonSysConfigKey;
import com.net.xpay.common.enums.user.LoginMethod;
import com.net.xpay.common.helper.CommonSysConfigHelper;
import com.net.xpay.common.model.RegisterModel;
import com.net.xpay.common.service.UserService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.enums.AlipayUserStatus;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.xpay.web.secutiry.FormLoginToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
  on 28/01/2018.
 * 登录 http://127.0.0.1:9093/common/login/form?mobile=18812345678&loginMethod=password&clientFrom=pc_web&password=123456&rememberMe=true
 */
@Slf4j
@RestController
@RequestMapping("/common/register")
@Api(tags = "公共/用户注册", description = "公共 用户注册 net")
public class RegisterController {
    @Autowired
    private UserService userService;
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private CommonSysConfigHelper commonSysConfigHelper;
    @Autowired
    private PartnerUserFilingsService partnerUserFilingsService;

    @ApiOperation(value = "注册")
    @PostMapping("/do-register")
    public ResultData<UserVo> doRegister(HttpServletRequest request, @RequestBody RegisterModel registerModel) {
        log.info("doRegister registerModel={}", SerializationUtil.obj2String(registerModel));
        registerModel.setIp(NetUtil.getRequestRealIp(request));
        Long openRegister = commonSysConfigHelper.getLong(CommonSysConfigKey.OPEN_REGISTER);
        if (openRegister == null || openRegister == 0L) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "系统升级，暂停开放注册");
        }
        User user = userService.doRegister(registerModel);
        autoLoginAfterRegister(request, user, registerModel);

        UserVo userVo = new UserVo();
        BeanCopierUtil.copy(user, userVo);
        userVo.setHasPayPassword(false);
        userVo.setAlipayUserStatus(AlipayUserStatus.NO);
        return new ResultData<>(userVo);
    }

    private void autoLoginAfterRegister(HttpServletRequest request, User user, RegisterModel registerModel) {
        FormLoginToken token = new FormLoginToken();
        token.setMobile(user.getMobile());
        token.setLoginMethod(LoginMethod.REGISTER_AUTO);
        token.setPassword(registerModel.getPassword());
        token.setClientFrom(registerModel.getClientFrom());

        token.setTime(LocalDateTime.now());
        token.setIp(NetUtil.getRequestRealIp(request));

        Authentication authentication = authenticationManager.authenticate(token);

        SecurityContextHolder.getContext().setAuthentication(authentication);
    }

    @ApiOperation("开户申请注册")
    @PostMapping("/partner-register")
    public ResultData<Boolean> search(@RequestBody PartnerUserFilingsSubmitModel model) {
        Partner partner = new Partner();
        partner.setMobile(model.getMobile());
        partner.setName(model.getBusinessName());
        partnerUserFilingsService.submitPartnerUserFilings(partner, model);
        return new ResultData<>(true);
    }
}
